<template>
  <div class="video-label-container">
    <el-row>
      <el-col :span="24">
        <el-button type="primary" @click="handleClick(null,0,'新增标签')">新增</el-button>
        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
          <el-form
            label-position="left"
            label-width="100px"
            :model="labelForm"
            :rules="rules"
            ref="labelForm"
          >
            <el-form-item prop="parents" label="上级标签">
              <el-cascader
                :disabled="formType === 1"
                placeholder="根目录"
                :options="rootLabels"
                v-model="labelForm.parents"
                :props="{ checkStrictly: true, label: 'name', value: 'id'}"
                clearable
              ></el-cascader>
            </el-form-item>
            <el-form-item label="名称" prop="name">
              <el-input
                :disabled="formType === 1"
                v-model="labelForm.name"
                placeholder="请输入内容"
                clearable
              />
            </el-form-item>
            <el-form-item prop="sort" label="排序">
              <el-input-number
                :disabled="formType === 1"
                v-model="labelForm.sort"
                :min="1"
                label="描述文字"
              ></el-input-number>
            </el-form-item>
            <el-form-item prop="status" label="启用">
              <el-switch
                :disabled="formType === 1"
                v-model="labelForm.status"
                active-value="0"
                inactive-value="1"
                active-color="#13ce66"
                inactive-color="#ff4949"
              ></el-switch>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button
              v-if="formType !== 1"
              :loading="loading"
              type="primary"
              @click="confirmHandel('labelForm')"
            >确 定</el-button>
          </span>
        </el-dialog>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table
          :data="pagination.categoryDatas"
          row-key="id"
          border
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column type="index" width="30px"></el-table-column>
          <el-table-column prop="name" label="名称"></el-table-column>
          <el-table-column prop="sort" label="排序"></el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.status === '0' ? 'success' : 'danger'"
                disable-transitions
              >{{scope.row.status === '0'? '启用': '禁用'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="150px">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row,1,'标签详情')" type="text" size="small">查看</el-button>
              <el-button @click="handleClick(scope.row,2,'编辑标签')" type="text" size="small">编辑</el-button>
              <el-button
                v-if="scope.row.sign !== 'SUPER_ADMIN'"
                @click="delLabel(scope.row)"
                type="text"
                size="small"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="pagination.pageSizeChoose"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
        ></el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  getPaginationList,
  getLabelList,
  add,
  delLabel,
  updateLabel,
} from "@/api/video/label";

import {cascaderDisabledSelf} from "@/utils/common"

export default {
  name: "VideoLabel",
  created() {
    this.getPaginationLabels();
  },
  data() {
    return {
      labelDatas: [],
      formType: 0,
      loading: false,
      rootLabels: [],
      dialogVisible: false,
      dialogTitle: "",
      labelForm: {
        parents: [],
        name: null,
        sort: 1,
        parentId: 0,
        status: "0",
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        pageSizeChoose: [5, 10, 15, 20],
        total: 0,
        categoryDatas: [],
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },

  watch: {
    dialogVisible: function () {
      if (this.dialogVisible === false) {
        this.$refs["labelForm"].resetFields();
      } else {
        getLabelList({ parentId: 0 }).then((response) => {
          this.rootLabels = response.data;
          cascaderDisabledSelf(this.rootLabels,this.labelForm)
        });
      }
    },
  },
  methods: {
    delLabel(role) {
      this.$confirm(
        '是否确认删除标签名为"' + role.name + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delLabel(role.id);
        })
        .then(() => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getPaginationLabels();
        })
        .catch(function () {});
    },
    addLabel() {
      this.loading = true;
      this.getParentId();
      add(this.labelForm)
        .then(() => {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.dialogVisible = false;
          this.loading = false;
          this.getPaginationLabels();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    updateLabel() {
      this.loading = true;
      this.getParentId();
      updateLabel(this.labelForm)
        .then(() => {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.dialogVisible = false;
          this.loading = false;
          this.getPaginationLabels();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    getParentId() {
      if (this.labelForm.parents.length > 0) {
        this.labelForm.parentId = this.labelForm.parents[
          this.labelForm.parents.length - 1
        ];
      }else{
        this.labelForm.parentId = null
      }
    },
    confirmHandel(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formType === 0) {
            this.addLabel();
          } else if (this.formType === 2) {
            this.updateLabel();
          }
          this.loading = true;
        }
      });
    },
    getPaginationLabels() {
      const params = {
        pageSize: this.pagination.pageSize,
        pageIndex: this.pagination.currentPage,
      };
      getPaginationList(params).then((res) => {
        this.pagination.categoryDatas = res.data.records;
        this.pagination.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val;
      this.getPaginationLabels();
    },
    handleCurrentChange(val) {
      this.pagination.currentPage = val;
      this.getPaginationLabels();
    },
    handleClick(data, type, title) {
      if (type !== 0) {
        let newData = JSON.parse(JSON.stringify(data));
        this.labelForm = newData;
        this.labelForm.parents = [];
        this.labelForm.parents.push(newData.parentId);
      }
      this.dialogTitle = title;
      this.formType = type;
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>